package com.fast.configuration;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by Le-Le on 2017/6/20.
 */

@Configuration
public class FastJsonConfiguration extends WebMvcConfigurerAdapter{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //调用父类的配置
        super.configureMessageConverters(converters);

        //创建消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //创建配置类
        FastJsonConfig fastConfig = new FastJsonConfig();

        //修改配置返回内容的过滤
        fastConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                                            SerializerFeature.WriteMapNullValue,
                                            SerializerFeature.WriteNullStringAsEmpty);
        fastConverter.setFastJsonConfig(fastConfig);
        converters.add(fastConverter);
    }
}
